package com.party.controller.sys.sysManage;

import com.party.model.SysModel;
import com.party.model.SysNav;
import com.party.model.SysNode;
import com.party.model.SysUser;
import com.party.service.sys.MenuService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 菜单管理
 * <p/>
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 12-3-13
 * Time: 下午3:08
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/sys/menus")
public class LeftMenusController {
    @Resource
    MenuService menuService;


    @RequestMapping(value = "left/{navId}")
    public ModelAndView left(
            @PathVariable(value = "navId") String navId,
            HttpSession session,
            HttpServletRequest request,
            HttpServletResponse response
    ) {
        ModelAndView modelAndView = new ModelAndView();

        String passport = "";
        if ((SysUser) session.getAttribute("sysUser") != null) {
            passport = ((SysUser) session.getAttribute("sysUser")).getPassport();
        }
        int status=0;

        StringBuffer stringBuffer = new StringBuffer();

        if (passport != null && !"".equals(passport)) {
            SysNav sysNav=menuService.getNav(Integer.parseInt(navId));
            List<SysModel> sysModels = menuService.getModelsByPassport(passport, Integer.parseInt(navId));
            if (sysModels.size() > 0) {
                for (SysModel sysModel : sysModels) {
                    if(status==0){
                        stringBuffer.append("<li class=\"last_li\"><a href=\"javascript:;\" class=\"nav-top-item current\">" + sysModel.getName() + "</a><ul>");
                    }else{
                        stringBuffer.append("<li class=\"last_li\"><a href=\"javascript:;\" class=\"nav-top-item \">" + sysModel.getName() + "</a><ul>");
                    }
                    status++;
                    List<SysNode> sysNodes = menuService.getNodesByPassport(passport, sysModel.getModelid());
                    for (SysNode sysNode : sysNodes) {
                        stringBuffer.append("<li><A class=menuChild href='" + sysNode.getCode() + "' target=main>" + sysNode.getName() + "</A></li>");
                    }
                    stringBuffer.append("</ul></li>");
                }
            }
            modelAndView.addObject("sysNav", sysNav.getName());
        }
        modelAndView.addObject("nodes", stringBuffer.toString());

        modelAndView.setViewName("/sys/leftMenu");
        return modelAndView;
    }
}
